import 'package:flutter/material.dart';
import 'package:flutter_baidu_map/contants/imageUrls.dart';
import 'package:flutter_baidu_map/loc/map_base_page_state.dart';
import 'package:flutter_bmfbase/BaiduMap/bmfmap_base.dart';
import 'package:flutter_bmfmap/BaiduMap/bmfmap_map.dart';

class MapPage extends StatefulWidget {
  MapPage({Key key}) : super(key: key);

  @override
  _MapPageState createState() => _MapPageState();
}

class _MapPageState extends BMFBaseMapState<MapPage> {
  BMFMapType mapType = BMFMapType.Standard; // 地图类型

  bool _isShowIndoorPoi = true; // 室内poi
  bool _isShowIndoorMap = true; // 室内地图
  BMFBaseIndoorMapInfo _indoorMapInfo;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Stack(
        children: <Widget>[
          generateMap(),
          generateControlBar(),
        ],
      ),
    );
  }

  /// 创建完成回调
  @override
  void onBMFMapCreated(BMFMapController controller) {
    super.onBMFMapCreated(controller);

    /// 地图View进入/移出室内图会调用此方法
    /// flag YES:进入室内图，false：移出室内图
    /// baseIndoorMapInfo 室内图信息
    myMapController?.setMapInOrOutBaseIndoorMapCallback(
        callback: (bool flag, BMFBaseIndoorMapInfo baseIndoorMapInfo) {
          setState(() {
            _indoorMapInfo = baseIndoorMapInfo;
            // _flag = flag;
          });
          print(
              '地图View进入/移出室内图会调用此方法\n flag=$flag + MapInfo=${baseIndoorMapInfo.toMap()}');
        }
    );
  }

  @override
  BMFMapOptions initMapOptions() {
    BMFCoordinate center = BMFCoordinate(34.732728, 113.629916);
    BMFMapOptions mapOptions = BMFMapOptions(
        mapType: BMFMapType.Standard, // 地图类型
        zoomLevel: 18, // 地图比例尺级别，在手机上当前可使用的级别为4-21级
        maxZoomLevel: 21,
        minZoomLevel: 4,
        logoPosition: BMFLogoPosition.LeftBottom, // logo位置 默认BMFLogoPositionLeftBottom
        mapPadding: BMFEdgeInsets(top: 0, left: 50, right: 50, bottom: 0),
        overlookEnabled: true, // 设定地图View能否支持俯仰角
        overlooking: -15, // 地图俯视角度，在手机上当前可使用的范围为－45～0度 (ios取int值)
        center: center,

        baseIndoorMapEnabled: _isShowIndoorMap,
        showIndoorMapPoi: _isShowIndoorPoi

    );
    return mapOptions;
  }

  @override
  Widget generateControlBar() {
    return Container(
        width: screenSize.width,
        height: 150,
        color: Color(0xD91F2131),
        child: Column(
          children: <Widget>[
            Row(
              mainAxisAlignment: MainAxisAlignment.spaceAround,
              children: <Widget>[
                Row(
                  children: <Widget>[
                    Radio(
                      value: BMFMapType.Standard,
                      groupValue: this.mapType,
                      onChanged: (value) {
                        myMapController.updateMapOptions(
                            BMFMapOptions(mapType: BMFMapType.Standard));
                        setState(() {
                          this.mapType = value;
                        });
                      },
                    ),
                    Text(
                      "普通地图",
                      style: TextStyle(color: Colors.white),
                    ),
                  ],
                ),
                SizedBox(
                  width: 0,
                ),

                Row(
                  children: <Widget>[
                    Radio(
                      value: BMFMapType.None,
                      groupValue: this.mapType,
                      onChanged: (value) {
                        myMapController.updateMapOptions(
                            BMFMapOptions(mapType: BMFMapType.None));
                        setState(() {
                          this.mapType = value;
                        });
                      },
                    ),
                    Text(
                      "空白地图",
                      style: TextStyle(color: Colors.white),
                    ),
                  ],
                )
              ],
            ),

            Row(
              mainAxisAlignment: MainAxisAlignment.center,
              children: <Widget>[
                Switch(
                    value: _isShowIndoorPoi,
                    activeColor: Colors.blue,
                    onChanged: (bool value) async {
                      setState(() {
                        _isShowIndoorPoi = value;
                      });
                      // 是否显示室内标注
                      await myMapController?.showBaseIndoorMapPoi(_isShowIndoorPoi);
                    }),
                Text(
                  '显示室内Poi',
                  textAlign: TextAlign.center,
                  style: TextStyle(color: Colors.white),
                ),
                SizedBox(width: 20),
                Switch(
                    value: _isShowIndoorMap,
                    activeColor: Colors.blue,
                    onChanged: (bool value) async {
                      setState(() {
                        _isShowIndoorMap = value;
                      });
                      // 是否展示室内地图
                      await myMapController?.showBaseIndoorMap(_isShowIndoorMap);

                      //室内图的开启和关闭会同时开启和关闭室内poi，所以这里重新设置下室内poi状态，保持室内poi显示和开关状态一致
                      await myMapController?.showBaseIndoorMapPoi(_isShowIndoorPoi);
                    }),
                Text(
                  '显示室内地图',
                  textAlign: TextAlign.center,
                  style: TextStyle(color: Colors.white),
                ),
              ],
            ),

            Row(
              children: <Widget>[
                FlatButton(
                  child: Text("添加点儿",style: TextStyle(color: Colors.white),),
                  onPressed: _addMarkerFun,
                ),

                RaisedButton(
                    shape: RoundedRectangleBorder(
                        borderRadius: BorderRadius.all(Radius.circular(10))),
                    color: Color(0xD91F2131),
                    textColor: Colors.white,
                    child: Text(
                      '改变地图中心点',
                      style: TextStyle(color: Colors.white),
                    ),
                    onPressed: () {
                      myMapController?.setNewLatLngZoom(
                          coordinate: BMFCoordinate(39.90, 116.40), zoom: 21,
                          animateDurationMs: 1000);
                    }),
              ],
            ),

          ],
        ),



    );
  }

  /// 创建BMFMarker
  BMFMarker marker = BMFMarker(
      position: BMFCoordinate(39.928617, 116.40329),
      title: 'flutterMaker',
      identifier: 'flutter_marker',
      icon: ImageUrls.BAIDUMAPLOGO);


  void _addMarkerFun(){
    /// 添加Marker
    myMapController?.addMarker(marker);
  }

}
